//
//  visitorLoginView.swift
//  Weibo10
//
//  Created by Leon on 15/10/14.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit

class visitorLoginView: UIView {
    
    // MARK: - 设置视图信息
    /**
    视图信息
    
    - parameter imageName: 首页设为nil，巧妙的更改
    - parameter title:     描述文字
    */
    func setupInfo(imageName: String?, title: String) {
        
        detailIntro.text = title
        
        guard let imgHeadName = imageName else {
            
            anim()
            return
        }
        
        headView.image = UIImage(named: imgHeadName)
        // 隐藏圈圈
        iconView.hidden = true
        // 把蒙层放到最下面
        sendSubviewToBack(maskIconView)
        
    }
    
    private func anim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 构造函数
    // 重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        // 这句话是关闭 sb或者xib的接口
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
        
    }

    //MARK: - 子空间的懒加载
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
     
    private lazy var headView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    private lazy var maskIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var detailIntro: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    

    lazy var registerBtn: UIButton = UIButton(title: "注册", titleColor: UIColor.orangeColor(), imageName: "common_button_white_disable")
    
    lazy var loginBrn: UIButton = UIButton(title: "登陆", titleColor: UIColor.blackColor(), imageName: "common_button_white_disable")     

}

// MARK: - 添加子控件的约束
extension visitorLoginView {
    
    func setupUI() {
        
        addSubview(iconView)
        // 遮罩 分析 在这两个之间
        addSubview(maskIconView)
        addSubview(headView)
        addSubview(detailIntro)
        addSubview(registerBtn)
        addSubview(loginBrn)

        // 圈子
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(snp_centerX)
            make.centerY.equalTo(snp_centerY).offset(-60)
        }
        // 小房子
        headView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        // 描述
        detailIntro.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.bottom.equalTo(iconView.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(224, 36))
        }
        // 注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(detailIntro.snp_left).offset(10)
            make.top.equalTo(detailIntro.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 36))
        }
        // 登陆按钮
        loginBrn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerBtn.snp_top)
            make.right.equalTo(detailIntro.snp_right).offset(-10)
            make.size.equalTo(registerBtn.snp_size)
        }

        // 遮罩
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.width.equalTo(self.snp_width)
            make.bottom.equalTo(loginBrn.snp_bottom)
        }
        
        // 在本页面，可以直接设置本页面的属性
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
    }
}














